package org.example.api.source;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;
import org.example.wordCount.WordCountByBatch;

import java.util.Properties;

/**
 * @author huangqihan
 * @date 2021/2/18
 */
public class SourceFromKafka {
    public static void main(String[] args) throws Exception {
        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // listen kafka events
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "localhost:9092");
        DataStreamSource<String> dataStreamSource = env.addSource(new FlinkKafkaConsumer011<String>("test", new SimpleStringSchema(), properties));

        // word count
        DataStream<Tuple2<String, Integer>> dataStream = dataStreamSource
                .flatMap(new WordCountByBatch.MyFlatMapper())
                // 按照当前数据的 hashCode 分组
                .keyBy(0)
                .sum(1);

        dataStream.print();

        env.execute();
    }
}
